﻿/*
  Project:       DataAnalyse
  File:          RequireItemsHandler.ashx
  Description:   
 *               The global verialbles are all in this file.
  Author:        Lanny(damon_lan@163.com)
  Date:          Sept,2014
  version:       2.0
  Modify:
  
  
  
  References:

 
                 
 Comments:
 * Add log function.


Notes:

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace DataAnalyseTool.BusinessTier.Handlers
{
  /// <summary>
  /// Summary description for RequiredItemsHandler
  /// </summary>
  public class RequiredItemsHandler : IHttpHandler
  {

    protected static readonly log4net.ILog log =
   log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      string action = context.Request.Params["action"];
      string result = "";
      try
      {
        switch (action)
        {
          case "Get":
            result = GetRequiredItems(context);
            break;
          case "Set":
            SetRequiredItems(context);
            break;
          default:
            break;
        }
        context.Response.Write(result);
      }
      catch (Exception ex)
      {
        log.Error("发生错误！详细信息是： "+ex.ToString());
      }

    }
    private string GetRequiredItems(HttpContext ctx)
    {
      string sb = "";
      string conbatPath = GlobalVar.configRelatePath + GlobalVar.isRequiredItemName + GlobalVar.xmlSubffix;
      XmlNodeList lists = GetNodeListFromXML(ctx.Server.MapPath(conbatPath), GlobalVar.xmlRequiredItemRootName);
      for (int i = 0; i < lists.Count; i++)
      {
        if (lists[i].Attributes["isRequired"].Value == "true")
        {
          sb += lists[i].Name + ";";
        }
      }
      if (sb != "")
      {
        sb = sb.Substring(0, sb.Length - 1);
      }
      return sb;
    }
    private void SetRequiredItems(HttpContext ctx)
    {
      string str = ctx.Request.QueryString["val"].ToString();
      if (str.Trim() == "")
      {
        ctx.Response.Write("没有设置任何必填字段，请重试！");
        log.Warn("没有设置任何必填字段，请重试！");
        return;
      }
      else
      {
        try
        {
          string conbatPath = GlobalVar.configRelatePath + GlobalVar.isRequiredItemName + GlobalVar.xmlSubffix;
          string filePath = ctx.Server.MapPath(conbatPath);
          str = str.Substring(0, str.Length - 1);
          string[] allItems = str.Split(';');

          XmlDocument xml = new XmlDocument();
          xml.Load(filePath);
          XmlNode root = xml.SelectSingleNode(GlobalVar.xmlRequiredItemRootName);
          XmlNodeList list = root.ChildNodes;
          SetAllProjectsFalse(list, xml, filePath);
          SetSomeProjectTrue(list, xml, filePath, allItems);
          ctx.Response.Write("设置成功！");
        }
        catch (Exception ex)
        {
          ctx.Response.Write("设置失败");
          log.Error("设置失败,详细信息是： "+ex.ToString());
        }
      }
    }

    private XmlNodeList GetNodeListFromXML(string path, string rootItem)
    {
      XmlDocument xml = new XmlDocument();
      xml.Load(path);
      XmlNode root = xml.SelectSingleNode(rootItem);
      XmlNodeList lists = root.ChildNodes;

      return lists;
    }

    private void SetAllProjectsFalse(XmlNodeList nodeList, XmlDocument xml, string path)
    {
      for (int i = 0; i < nodeList.Count; i++)
      {
        XmlElement ement = (XmlElement)nodeList[i];
        ement.SetAttribute("isRequired", "false");
      }
      xml.Save(path);
    }
    private void SetSomeProjectTrue(XmlNodeList nodeList, XmlDocument xml, string path, string[] allItems)
    {
      for (int i = 0; i < nodeList.Count; i++)
      {
        for (int j = 0; j < allItems.Length; j++)
        {
          if (nodeList[i].Name == allItems[j])
          {
            XmlElement xe = (XmlElement)nodeList[i];
            xe.SetAttribute("isRequired", "true");
          }
        }
      }
      xml.Save(path);
    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}